<?php

use Biz\module\Sys\ErrorConst;
use Biz\service\course\CourseOrder as sCourseOrder;
use Biz\service\package\PackageOrder;
use Biz\service\trade\Order;
use Biz\service\trade\Pay;
use Doraemon\tools\ActionLog;

class CreateOrderAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        $orderNo = $this->getRequest()->getPost('order_no');
        $payWay = $this->getRequest()->getPost('pay_way');
        $openId = $this->getRequest()->getPost('open_id');
        $orderType = $this->getRequest()->getPost('order_type', Pay::_PAY_ORDER_TYPE_BIZ);
        if (!in_array($orderType, [
            Pay::_PAY_ORDER_TYPE_BIZ,
            Pay::_PAY_ORDER_TYPE_COURSE,
            Pay::_PAY_ORDER_TYPE_PACKAGE
        ])) {
            throw new \Exception('参数错误');
        }
        if ($orderNo) {
            if ($orderType == Pay::_PAY_ORDER_TYPE_BIZ) {
                $orderInfo = Order::getCashierAmount($orderNo);
                if ($orderInfo['uid'] != $this->uid) {
                    throw new \Exception('订单信息异常', ErrorConst::ERROR_PAY_ORDER_INFO_UNKNOW);
                } else {
                    $isPaid = Pay::checkOrderPayStatus($orderNo);
                    if ($isPaid) {
                        throw new \Exception('订单已付款', ErrorConst::ERROR_PAY_ORDER_INFO_PAID);
                    }
                }
            } else if ($orderType == Pay::_PAY_ORDER_TYPE_COURSE) {
                $orderInfo = sCourseOrder::getCashierAmount($orderNo);
                if ($orderInfo['uid'] != $this->uid) {
                    throw new \Exception('订单信息异常', ErrorConst::ERROR_PAY_ORDER_INFO_UNKNOW);
                } else {
                    $isPaid = Pay::checkOrderPayStatus($orderNo, Pay::_PAY_ORDER_TYPE_COURSE);
                    if ($isPaid) {
                        throw new \Exception('订单已付款', ErrorConst::ERROR_PAY_ORDER_INFO_PAID);
                    }
                }
                ActionLog::log($this->uid, [
                    'order_no'  => $orderNo,
                    'course_id' => $orderInfo['course_id'],
                    'addtime'   => date('Y-m-d H:i:s')
                ], ActionLog::BIZ_COURSE_PAY_ORDER);
            } elseif ($orderType == Pay::_PAY_ORDER_TYPE_PACKAGE) {
                $uid = PackageOrder::getUidByOrderNo($orderNo);
                $orderInfo = PackageOrder::getCashierAmount($orderNo);
                if ($uid != $this->uid) {
                    throw new \Exception('订单信息异常', ErrorConst::ERROR_PAY_ORDER_INFO_UNKNOW);
                } else {
                    $isPaid = Pay::checkOrderPayStatus($orderNo, Pay::_PAY_ORDER_TYPE_COURSE);
                    if ($isPaid) {
                        throw new \Exception('订单已付款', ErrorConst::ERROR_PAY_ORDER_INFO_PAID);
                    }
                }
            }
        } else {
            throw new \Exception('订单信息异常', ErrorConst::ERROR_PAY_ORDER_INFO_UNKNOW);
        }
        $orderInfo['open_id'] = $openId;
        $payOrderInfo = Pay::getPayOrderInfo($orderInfo, $payWay, $orderType);
        //兼容付费课微信支付账号更换为新账号，android不兼容的问题
        if ($orderType == Pay::_PAY_ORDER_TYPE_COURSE && $payWay == Pay::_PAY_WAYS_WECHAT_BIZ) {
            $payWay = Pay::_PAY_WAYS_WECHAT;
        }
        if ($orderType == Pay::_PAY_ORDER_TYPE_PACKAGE && $payWay == Pay::_PAY_WAYS_WECHAT_BIZ) {
            $payWay = Pay::_PAY_WAYS_WECHAT;
        }
        $result = [
            'pay_way'               => $payWay,
            $payWay . '_order_info' => $payOrderInfo
        ];
        $this->setSuccess($result);
    }
}